package com.dashunwms_aohe.service.impl;

import com.alibaba.fastjson.JSON;
import com.dashunwms_aohe.bean.Qf_BillType;
import com.dashunwms_aohe.bean.ZxyUploadbillDS;
import com.dashunwms_aohe.bean.ds.DS_Url;
import com.dashunwms_aohe.bean.ds.UploadDSBillDetails;
import com.dashunwms_aohe.bean.ds.UploadDSBillIndex;
import com.dashunwms_aohe.dao.InStockBillDao;
import com.dashunwms_aohe.dao.OutStockBillDao;
import com.dashunwms_aohe.dao.ZxyUploadbillDSDao;
import com.dashunwms_aohe.http.DSHttpService;
import com.dashunwms_aohe.http.res.UploadBillRes;
import com.dashunwms_aohe.service.UploadBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j(topic = "UploadBillServiceImpl")
public class UploadBillServiceImpl implements UploadBillService {
    @Resource
    private InStockBillDao inStockBillDao;
    @Resource
    private OutStockBillDao outStockBillDao;
    @Resource
    private DSHttpService httpService;
    @Resource
    private ZxyUploadbillDSDao zxyUploadbillDSDao;
    @Override
    public UploadBillRes uploadBuyBillToDS(String billCode) {
        UploadDSBillIndex qfBuyBillIndex = inStockBillDao.getQfBuyBillIndex(billCode);
        List<UploadDSBillDetails> qfBuyBillDetails = inStockBillDao.getQfBuyBillDetails(billCode);
        for (UploadDSBillDetails qfBuyBillDetail : qfBuyBillDetails) {
            qfBuyBillIndex.getAllLogisticsOrderL().add(qfBuyBillDetail);
        }
        UploadBillRes uploadBillRes = httpService.HttpPostReq_UploadBill(DS_Url.UPLOADBILL_URL, JSON.toJSONString(qfBuyBillIndex));
        if (uploadBillRes.getResponse().getRequest()) {
            for (UploadDSBillDetails qfBuyBillDetail : qfBuyBillDetails) {
                ZxyUploadbillDS zxyUploadbillYD = new ZxyUploadbillDS();
                zxyUploadbillYD.setBillcode(billCode);
                zxyUploadbillYD.setOrd(Integer.parseInt(qfBuyBillDetail.getLol_owner_system_line_no()));
                zxyUploadbillYD.setBilltype(Qf_BillType.QF_Buy);
                zxyUploadbillDSDao.updateFlags(zxyUploadbillYD);
            }
        }
        return uploadBillRes;
    }

    @Override
    public UploadBillRes uploadBuyBackBillToDS(String billCode) {
        UploadDSBillIndex qfBuyBackBillIndex = outStockBillDao.getQfBuyBackBillIndex(billCode);
        List<UploadDSBillDetails> qfBuyBackBillDetails = outStockBillDao.getQfBuyBackBillDetails(billCode);
        for (UploadDSBillDetails qfBuyBackBillDetail : qfBuyBackBillDetails) {
            qfBuyBackBillIndex.getAllLogisticsOrderL().add(qfBuyBackBillDetail);
        }
        UploadBillRes uploadBillRes = httpService.HttpPostReq_UploadBill(DS_Url.UPLOADBILL_URL, JSON.toJSONString(qfBuyBackBillIndex));
        if (uploadBillRes.getResponse().getRequest()) {
            for (UploadDSBillDetails qfBuyBillDetail : qfBuyBackBillDetails) {
                ZxyUploadbillDS zxyUploadbillYD = new ZxyUploadbillDS();
                zxyUploadbillYD.setBillcode(billCode);
                zxyUploadbillYD.setOrd(Integer.parseInt(qfBuyBillDetail.getLol_owner_system_line_no()));
                zxyUploadbillYD.setBilltype(Qf_BillType.QF_BuyBack);
                zxyUploadbillDSDao.updateFlags(zxyUploadbillYD);
            }
            outStockBillDao.updateGspOutBillOutQty0(billCode);
        }
        return uploadBillRes;
    }

    @Override
    public UploadBillRes uploadSaleBillToDS(String billCode) {
        UploadDSBillIndex qfSaleBillIndex = outStockBillDao.getQfSaleBillIndex(billCode);
        List<UploadDSBillDetails> qfSaleBillDetails = outStockBillDao.getQfSaleBillDetails(billCode);
        for (UploadDSBillDetails qfSaleBillDetail : qfSaleBillDetails) {
            qfSaleBillIndex.getAllLogisticsOrderL().add(qfSaleBillDetail);
        }
        UploadBillRes uploadBillRes = httpService.HttpPostReq_UploadBill(DS_Url.UPLOADBILL_URL, JSON.toJSONString(qfSaleBillIndex));
        if (uploadBillRes.getResponse().getRequest()) {
            for (UploadDSBillDetails qfBuyBillDetail : qfSaleBillDetails) {
                ZxyUploadbillDS zxyUploadbillYD = new ZxyUploadbillDS();
                zxyUploadbillYD.setBillcode(billCode);
                zxyUploadbillYD.setOrd(Integer.parseInt(qfBuyBillDetail.getLol_owner_system_line_no()));
                zxyUploadbillYD.setBilltype(Qf_BillType.QF_Sale);
                zxyUploadbillDSDao.updateFlags(zxyUploadbillYD);
            }
            outStockBillDao.updateGspOutBillOutQty0(billCode);
        }
        return uploadBillRes;
    }

    @Override
    public UploadBillRes uploadSaleBackBillToDS(String billCode) {
        UploadDSBillIndex qfSaleBackBillIndex = inStockBillDao.getQfSaleBackBillIndex(billCode);
        List<UploadDSBillDetails> qfSaleBackBillDetails = inStockBillDao.getQfSaleBackBillDetails(billCode);
        for (UploadDSBillDetails qfSaleBackBillDetail : qfSaleBackBillDetails) {
            qfSaleBackBillIndex.getAllLogisticsOrderL().add(qfSaleBackBillDetail);
        }
        UploadBillRes uploadBillRes = httpService.HttpPostReq_UploadBill(DS_Url.UPLOADBILL_URL, JSON.toJSONString(qfSaleBackBillIndex));
        if (uploadBillRes.getResponse().getRequest()) {
            for (UploadDSBillDetails qfBuyBillDetail : qfSaleBackBillDetails) {
                ZxyUploadbillDS zxyUploadbillYD = new ZxyUploadbillDS();
                zxyUploadbillYD.setBillcode(billCode);
                zxyUploadbillYD.setOrd(Integer.parseInt(qfBuyBillDetail.getLol_owner_system_line_no()));
                zxyUploadbillYD.setBilltype(Qf_BillType.QF_SaleBack);
                zxyUploadbillDSDao.updateFlags(zxyUploadbillYD);
            }
        }
        return uploadBillRes;
    }
}
